import {Component, Input, OnInit} from "@angular/core";
import {Hero} from "../../models/hero";
import {ActivatedRoute, Params} from "@angular/router";
import {HeroesService} from "../../services/heroes.service";
import {Location} from "@angular/common";

@Component({
  selector: 'hero-detail',
  templateUrl: './hero-detail.component.html',
  styleUrls: ['./hero-detail.component.css'],
  providers: []
})
export class HeroDetailComponent implements OnInit {
  @Input()
  hero: Hero

  _hero: Hero

  get item() {
    return this.hero || this._hero
  }

  constructor(private heroService: HeroesService,
              private route: ActivatedRoute,
              private location: Location) {

  }

  ngOnInit() {
    if (!this.hero) {
      this.route.params.forEach((params: Params) => {
        let id = +params['id']
        this.heroService.getHero(id).subscribe(hero => this._hero = hero)
      })
    }
  }

  // goBack() {
  //   this.location.back()
  // }
}
